package internal

import (
	"wechat-push/internal/model"
	"wechat-push/internal/repository"
	"wechat-push/pkg/logger"
	"wechat-push/pkg/util"
)

// GetRegionCode 获取和风天气地区对应的代码
func GetRegionCode(region, city string) (regionCode string, err error) {
	resp, err := repository.GetRegionCode(region)
	if err != nil {
		logger.Log.Error("GetRegionCode err :", err)
		return
	}
	// 遍历找到regionId
	for _, res := range resp.Location {
		if res.Adm2 == city {
			regionCode = res.Id
			return
		}
	}
	return
}

// GetRealtimeWeather 获取实时天气
func GetRealtimeWeather(regionCode string) (weather model.RealtimeWeather, err error) {
	resp, err := repository.GetCurrentWeather(regionCode)
	if err != nil {
		logger.Log.Error("GetRealtimeWeather err :", err)
		return
	}
	weather.Text = resp.Now.Text
	weather.Temp = resp.Now.Temp
	weather.WindDir = resp.Now.WindDir
	weather.WindScale = resp.Now.WindScale
	weather.Vis = resp.Now.Vis
	return
}

// 获取今天的天气预报(最低、最高温度)
func GetWeatherForecast(regionCode string) (forecast model.ForecastWeather, err error) {
	resp, err := repository.GetForecastWeather(regionCode)
	if err != nil {
		logger.Log.Error("GetForecastWeather err :", err)
		return
	}
	today := util.GetToday()
	for _, day := range resp.Daily {
		if day.FxDate == today {
			forecast.MaxTemp = day.TempMax
			forecast.MinTemp = day.TempMin
		}
	}
	return
}

// 获取今天的空气质量状况
func GetAirQuality(regionCode string) (airQuality string, err error) {
	resp, err := repository.GetAirQuality(regionCode)
	if err != nil {
		logger.Log.Error("GetAirQuality err :", err)
		return
	}
	airQuality = resp.Now.Category
	return
}
